package cn.iocoder.yudao.module.promotion.groupon.controller.admin;

import cn.iocoder.yudao.framework.business.basic.controller.CreateController;
import cn.iocoder.yudao.framework.business.basic.controller.DeleteController;
import cn.iocoder.yudao.framework.business.basic.controller.DetailController;
import cn.iocoder.yudao.framework.business.basic.controller.UpdateController;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.promotion.groupon.convert.GrouponProductConvert;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.AdminGrouponProductRespVO;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.AppGrouponProductRespVO;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.GrouponProductSaveDTO;
import cn.iocoder.yudao.module.promotion.groupon.service.app.AppGrouponProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 团购产品")
@RestController
@RequestMapping("/promotion/groupon-product")
@Validated
public class GrouponProductController implements CreateController<GrouponProductSaveDTO>, UpdateController<GrouponProductSaveDTO>, DeleteController<Long>, DetailController<AdminGrouponProductRespVO, Long> {

    @Resource
    private AppGrouponProductService appGrouponProductService;

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Long> create(GrouponProductSaveDTO param) {
        return success(appGrouponProductService.create(param, getLoginUserId()));
    }

    @PreAuthorize("@ss.hasAnyRoles('shop_admin','super_admin', 'platform_admin', 'tenant_admin')")
    @Override
    public CommonResult<Boolean> update(GrouponProductSaveDTO param) {
        return success(appGrouponProductService.update(param, getLoginUserId()));
    }

    @PreAuthorize("@ss.hasRole('shop_admin')")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return success(appGrouponProductService.delete(id, getLoginUserId()));
    }

    @PreAuthorize("@ss.hasPermission('promotion:groupon:package')")
    @Override
    public CommonResult<AdminGrouponProductRespVO> getDetail(Long id) {
        AppGrouponProductRespVO respVO = appGrouponProductService.getDetail(id);
        return success(GrouponProductConvert.INSTANCE.convert2(respVO));
    }

    @PreAuthorize("@ss.hasPermission('promotion:groupon:package')")
    @Operation(summary = "列表")
    @GetMapping("/list")
    public CommonResult<List<AdminGrouponProductRespVO>> getList(@RequestParam("packageId") Long packageId) {
        List<AppGrouponProductRespVO> list = appGrouponProductService.getListByPackageId(packageId);
        return success(GrouponProductConvert.INSTANCE.convertList2(list));
    }

}
